<!-- eslint-disable vue/valid-v-slot -->
<template>
  <div class="main-container">
    <TableBody>
      <template #header>
        <TableHeader ref="tableHeaderRef" :show-filter="false">
          <template #search-content>
            <a-form ref="serchForm" :model="form">
              <a-row :gutter="24">
                <a-col :span="4">
                  <!-- <Inputs :cards="form.cards" @update="cardFn" /> -->
                  <a-form-item field="cards" hide-label>
                    <a-input v-model="form.cards" placeholder="请输入卡号">
                      <template #suffix>
                        <icon-plus @click="open" style="cursor: pointer" />
                      </template>
                    </a-input>
                  </a-form-item>
                </a-col>
                <a-col :span="4">
                  <a-form-item field="remarks" hide-label>
                    <a-input v-model="form.remarks" placeholder="请输入订单号" />
                  </a-form-item>
                </a-col>
                <a-col :span="4">
                  <a-form-item field="channelId" hide-label>
                    <a-select
                      v-model="form.channelId"
                      placeholder="请选择渠道"
                      allow-clear
                      allow-search
                    >
                      <a-option v-for="item in channel" :key="item.id" :value="item.id">{{
                        item.name
                      }}</a-option>
                    </a-select>
                  </a-form-item>
                </a-col>
                <a-col :span="4">
                  <a-form-item field="seriesId" hide-label>
                    <a-select
                      v-model="form.seriesId"
                      placeholder="请选择系列"
                      allow-clear
                      allow-search
                    >
                      <a-option v-for="item in series" :key="item.id" :value="item.id">{{
                        item.name
                      }}</a-option>
                    </a-select>
                  </a-form-item>
                </a-col>
                <a-col :span="4">
                  <a-form-item field="add" hide-label>
                    <a-select v-model="form.add" placeholder="请选择余额增减" allow-clear>
                      <a-option value="0">减少</a-option>
                      <a-option value="1">增加</a-option>
                    </a-select>
                  </a-form-item>
                </a-col>
                <a-col :span="4">
                  <a-form-item field="type" hide-label>
                    <a-select
                      v-model="form.type"
                      placeholder="请选择账单类型"
                      allow-clear
                      allow-search
                    >
                      <a-option
                        v-for="(item, index) in typeLists"
                        :key="index"
                        :value="item.type"
                        >{{ item.message }}</a-option
                      >
                    </a-select>
                  </a-form-item>
                </a-col>
                <a-col :span="4">
                  <a-form-item field="addTime" hide-label>
                    <a-date-picker
                      v-model="form.addTime"
                      style="width: 100%"
                      show-time
                      placeholder="请选择开始时间"
                      :shortcuts="[
                        {
                          label: '今日',
                          value: () => (
                            (form.addTime = $dayjs('00:00:00', 'HH:mm:ss').format(
                              'YYYY-MM-DD HH:mm:ss'
                            )),
                            (form.endTime = $dayjs('23:59:59', 'HH:mm:ss').format(
                              'YYYY-MM-DD HH:mm:ss'
                            ))
                          ),
                        },
                        {
                          label: '昨日',
                          value: () => (
                            (form.addTime = $dayjs('00:00:00', 'HH:mm:ss')
                              .subtract(1, 'day')
                              .format('YYYY-MM-DD HH:mm:ss')),
                            (form.endTime = $dayjs('23:59:59', 'HH:mm:ss')
                              .subtract(1, 'day')
                              .format('YYYY-MM-DD HH:mm:ss'))
                          ),
                        },
                        {
                          label: '七日',
                          value: () => (
                            (form.addTime = $dayjs('00:00:00', 'HH:mm:ss')
                              .subtract(6, 'day')
                              .format('YYYY-MM-DD HH:mm:ss')),
                            (form.endTime = $dayjs().format('YYYY-MM-DD HH:mm:ss'))
                          ),
                        },
                        {
                          label: '本月',
                          value: () => (
                            (form.addTime = $dayjs('01 00:00:00', 'DD HH:mm:ss').format(
                              'YYYY-MM-DD HH:mm:ss'
                            )),
                            (form.endTime = $dayjs().format('YYYY-MM-DD HH:mm:ss'))
                          ),
                        },
                        {
                          label: '上月',
                          value: () => (
                            (form.addTime = $dayjs(`01 00:00:00`, 'DD HH:mm:ss')
                              .subtract(1, 'month')
                              .format('YYYY-MM-DD HH:mm:ss')),
                            (form.endTime = $dayjs()
                              .subtract(1, 'month')
                              .endOf('month')
                              .format('YYYY-MM-DD HH:mm:ss'))
                          ),
                        },
                      ]"
                      :time-picker-props="{ defaultValue: '12:00:00' }"
                      format="YYYY-MM-DD HH:mm:ss"
                      allow-clear
                    />
                  </a-form-item>
                </a-col>
                <a-col :span="4">
                  <a-form-item field="endTime" hide-label>
                    <a-date-picker
                      v-model="form.endTime"
                      style="width: 100%"
                      show-time
                      placeholder="请选择结束时间"
                      :shortcuts="[
                        {
                          label: '今日',
                          value: () => (
                            (form.addTime = $dayjs('00:00:00', 'HH:mm:ss').format(
                              'YYYY-MM-DD HH:mm:ss'
                            )),
                            (form.endTime = $dayjs('23:59:59', 'HH:mm:ss').format(
                              'YYYY-MM-DD HH:mm:ss'
                            ))
                          ),
                        },
                        {
                          label: '昨日',
                          value: () => (
                            (form.addTime = $dayjs('00:00:00', 'HH:mm:ss')
                              .subtract(1, 'day')
                              .format('YYYY-MM-DD HH:mm:ss')),
                            (form.endTime = $dayjs('23:59:59', 'HH:mm:ss')
                              .subtract(1, 'day')
                              .format('YYYY-MM-DD HH:mm:ss'))
                          ),
                        },
                        {
                          label: '七日',
                          value: () => (
                            (form.addTime = $dayjs('00:00:00', 'HH:mm:ss')
                              .subtract(6, 'day')
                              .format('YYYY-MM-DD HH:mm:ss')),
                            (form.endTime = $dayjs().format('YYYY-MM-DD HH:mm:ss'))
                          ),
                        },
                        {
                          label: '本月',
                          value: () => (
                            (form.addTime = $dayjs('01 00:00:00', 'DD HH:mm:ss').format(
                              'YYYY-MM-DD HH:mm:ss'
                            )),
                            (form.endTime = $dayjs().format('YYYY-MM-DD HH:mm:ss'))
                          ),
                        },
                        {
                          label: '上月',
                          value: () => (
                            (form.addTime = $dayjs(`01 00:00:00`, 'DD HH:mm:ss')
                              .subtract(1, 'month')
                              .format('YYYY-MM-DD HH:mm:ss')),
                            (form.endTime = $dayjs()
                              .subtract(1, 'month')
                              .endOf('month')
                              .format('YYYY-MM-DD HH:mm:ss'))
                          ),
                        },
                      ]"
                      :time-picker-props="{ defaultValue: '12:00:00' }"
                      format="YYYY-MM-DD HH:mm:ss"
                      allow-clear
                    />
                  </a-form-item>
                </a-col>
                <a-col :span="4">
                  <a-form-item field="btns" hide-label>
                    <a-space :size="15">
                      <a-button
                        type="primary"
                        v-permission="billCard"
                        html-type="submit"
                        @click="doRefresh(1)"
                        ><template #icon>
                          <icon-search />
                        </template>
                        <!-- Use the default slot to avoid extra spaces -->
                        <template #default>搜索</template></a-button
                      >
                      <a-button v-permission="billCard" @click="resetSeach"
                        ><template #icon>
                          <icon-sync theme="outline" size="14" />
                        </template>
                        <!-- Use the default slot to avoid extra spaces -->
                        <template #default>重置</template></a-button
                      >
                    </a-space>
                  </a-form-item>
                </a-col>
              </a-row>
            </a-form>
          </template>
        </TableHeader>
      </template>
      <template #table-config>
        <a-space :size="15">
          <a-button
            type="outline"
            v-permission="exportBillCard"
            @click="exportLists"
            :loading="loading"
          >
            <!-- Use the default slot to avoid extra spaces -->
            <template #default>导出</template>
          </a-button>
        </a-space>
        <a-space :size="15">
          <TableConfig
            @update-border="onUpdateBorder"
            @update-striped="onUpdateStriped"
            @refresh="doRefresh"
          />
        </a-space>
      </template>
      <template #default>
        <a-table
          :bordered="{ wrapper: true, cell: bordered }"
          :stripe="(striped as boolean)"
          :loading="tableLoading"
          :data="list"
          :pagination="false"
          :column-resizable="true"
          :rowKey="rowKey"
          :columns="tableColumns.list"
          :scrollbar="true"
          :scroll="{ x: 1000 }"
          table-layout-fixed
        >
          <template #columns>
            <a-table-column
              v-for="item of tableColumns.list"
              :key="item.key"
              align="left"
              :width="item.width"
              :ellipsis="true"
              :tooltip="true"
              :title="(item.title as string)"
              :data-index="(item.key as string)"
              :fixed="item.fixed"
            >
              <template v-if="item.key === 'remarks'" #cell="{ record }">
                <span v-if="record.remarks">{{ record.remarks }}</span>
                <span v-else>---</span>
              </template>
              <template v-else-if="item.key === 'add'" #cell="{ record }">
                <a-tag color="blue" v-if="record.add === true">增加</a-tag>
                <a-tag color="red" v-else-if="record.add === false">减少</a-tag>
              </template>
              <template v-else-if="item.key === 'type'" #cell="{ record }">
                <span
                  v-for="(items, index) in typeLists"
                  :key="index"
                  v-show="items.type == record.type"
                  >{{ items.message }}</span
                >
              </template>
            </a-table-column>
          </template>
        </a-table>
      </template>
      <template #footer>
        <TableFooter ref="tableFooterRef" :pagination="pagination" />
      </template>
    </TableBody>
    <!-- 多卡搜索 -->
    <a-modal
      v-model:visible="cardsVisible"
      @before-close="colse"
      title="多条件搜索"
      width="400px"
      :mask-closable="false"
      title-align="start"
    >
      <a-textarea
        style="width: 100%"
        v-model="card"
        placeholder="请输入搜索条件，一行一个"
        allow-clear
      />
      <template #footer>
        <a-space>
          <a-button @click="cardsVisible = false">取消</a-button>
          <a-button type="primary" @click="cardsFn">确定</a-button>
        </a-space>
      </template>
    </a-modal>
  </div>
</template>

<script lang="ts" setup>
  import Inputs from '@/views/finance/components/input.vue'
  import * as http from '@/api/http'
  import { formpost, get } from '@/api/http'
  import { billCard, billName, exportBillCard } from '@/api/url'
  import useCommonStore from '@/store/modules/common'
  import { usePagination, useRowKey, useTable, useTableHeight } from '@/hooks/table'
  import { defineComponent, getCurrentInstance, onMounted, reactive, ref } from 'vue'

  const form = reactive({
    remarks: '',
    card: '',
    type: '',
    add: '',
    channelId: '',
    seriesId: '',
    addTime: '',
    endTime: '',
    cards: '',
  })
  const channel: any = ref([])
  const series: any = ref([])
  const userCommon = useCommonStore()
  const typeLists: any = ref([])
  const table = useTable()
  const rowKey = useRowKey('id')
  const pagination = usePagination(doRefresh)
  const userLists: any = ref([])
  const tableColumns: any = reactive({
    list: [
      {
        title: '卡号',
        key: 'card',
        dataIndex: 'card',
        checked: true,
        width: 270,
      },
      {
        title: '交易前余额',
        key: 'addBalance',
        dataIndex: 'addBalance',
        checked: true,
        width: 120,
      },
      {
        title: '交易余额',
        key: 'balance',
        dataIndex: 'balance',
        checked: true,
        width: 120,
      },
      {
        title: '交易后余额',
        key: 'endBalance',
        dataIndex: 'endBalance',
        checked: true,
        width: 120,
      },
      {
        title: '类型',
        key: 'type',
        dataIndex: 'type',
        checked: true,
        width: 120,
      },
      {
        title: '交易备注',
        key: 'remarks',
        dataIndex: 'remarks',
        checked: true,
        width: 120,
      },
      {
        title: '状态',
        key: 'add',
        dataIndex: 'add',
        checked: true,
        width: 120,
      },
      {
        title: '交易时间',
        key: 'createTime',
        dataIndex: 'createTime',
        checked: true,
        width: 190,
      },
    ],
  })
  const expandAllFlag = ref(true)
  const tableLoading = ref(true)
  const btnLoading = ref(false)
  const visible = ref(false)
  const loading = ref(false)
  const list = ref([])
  const cardsVisible = ref(false)
  const card = ref('')
  const open = () => {
    cardsVisible.value = true
  }
  function cardsFn() {
    const arr = card.value.split(/\n/g)
    form.cards = arr.toString()
    cardsVisible.value = false
  }
  const colse = () => {
    cardsVisible.value = false
  }
  const exportLists = () => {
    loading.value = true
    userCommon.downLoadPosts(exportBillCard, form, '卡板明细列表').then(() => {
      loading.value = false
    })
  }
  //获取多卡
  function cardFn(value: any) {
    form.cards = value
  }
  function getTypeList() {
    http
      .get({
        url: billName,
        data: () => {
          return {}
        },
      })
      .then((res) => {
        typeLists.value = res.data
      })
  }
  function doRefresh(value?: number) {
    // eslint-disable-next-line prettier/prettier
    tableLoading.value = true
    if (value) {
      pagination.page = value
    }
    formpost({
      url: billCard,
      data: () => {
        return {
          page: pagination.page,
          size: pagination.pageSize,
          ...form,
        }
      },
    })
      .then((res) => {
        tableLoading.value = false
        list.value = res.data.list
        pagination.setTotalSize((res.data as any).totalSize)
      })
      .catch(console.log)
  }

  function onUpdateBorder(isBordered: boolean) {
    table.bordered.value = isBordered
  }
  function onUpdateStriped(isStriped: boolean) {
    table.striped.value = isStriped
  }
  function getchannel() {
    userCommon.channels().then((res: any) => {
      channel.value = res.data
    })
  }
  function getseries() {
    userCommon.seriess().then((res: any) => {
      series.value = res.data
    })
  }
  const serchForm = ref()
  const resetSeach = () => {
    card.value = ''
    serchForm.value?.resetFields()
    doRefresh(1)
  }
  onMounted(async () => {
    table.tableHeight.value = await useTableHeight(getCurrentInstance())
    doRefresh()
    getTypeList()
    getseries()
    getchannel()
  })
</script>
<style lang="less" scoped>
  :deep(.arco-modal) {
    .arco-modal-footer {
      text-align: right !important;
    }
  }
  :deep(.arco-modal-header .arco-modal-title) {
    display: flex;
    justify-content: flex-start !important;
  }
</style>
